package com.algorithm.link;/*
 *@Author:DOWN
 *@Date:2021/12/29 9:31
 *@Comment:单链表
 */

public class LinkedList<E> {
    /**
     * 私有节点类
     */
    private class Node {
        //元素
        public E item;
        //下级
        public Node next;

        public Node(E newItem, Node newNext) {
            this.item = newItem;
            this.next = newNext;
        }

        public Node(E newItem) {
            this(newItem, null);
        }

        public Node() {
            this(null, null);
        }

        @Override
        public String toString() {
            return item.toString();
        }
    }

    /**
     * 虚头节点 -1
     */
    private Node dummyHead;
    /**
     * 节点长度
     */
    private int size;

    /**
     * 初始化时：虚节点 为空
     */
    public LinkedList() {
        dummyHead = new Node(null, null);
        size = 0;
    }

    /**
     * 元素长度
     *
     * @return int
     */
    public int getSize() {
        return size;
    }

    /**
     * 空判断
     *
     * @return boolean
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 添加元素
     *
     * @param index 位置
     * @param e     元素
     */
    public void add(int index, E e) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Illegal index.");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        prev.next = new Node(e, prev.next);
        size++;
    }

    /**
     * 添加元素至第一个位置
     *
     * @param e 元素
     */
    public void addFirst(E e) {
        add(0, e);
    }

    /**
     * 添加元素至最后一个位置
     *
     * @param e 元素
     */
    public void addLast(E e) {
        add(size, e);
    }

    /**
     * 删除元素
     *
     * @param index 位置
     * @return 元素
     */
    public E remove(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("Add failed. Illegal index.");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node del = prev.next;
        prev.next = del.next;
        del.next = null;
        size--;
        return del.item;
    }

    /**
     * 删除第一个元素
     *
     * @return 元素
     */
    public E removeFirst() {
        return remove(0);
    }

    /**
     * 删除最后一个元素
     *
     * @return 元素
     */
    public E removeLast(int index) {
        return remove(0);
    }

    /**
     * 删除指定元素
     *
     * @param e 元素
     * @return boolean
     */
    public boolean removeElenment(E e) {
        if (size <= 0) {
            throw new IllegalArgumentException("find failed. Illegal index.");
        }
        Node prev = dummyHead;
        for (int i = 0; i < size; i++) {
            if (prev.next.item == e) {
                remove(i);
                return true;
            }
            prev = prev.next;
        }
        return false;
    }

    /**
     * 查找元素
     *
     * @param index 位置
     * @return 元素
     */
    public E find(int index) {
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("find failed. Illegal index.");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        return prev.next.item;
    }

    /**
     * 是否包含元素
     *
     * @param e 元素
     * @return boolean
     */
    public boolean contains(E e) {
        if (size <= 0) {
            throw new IllegalArgumentException("contains failed. Illegal index.");
        }
        Node prev = dummyHead;
        while (prev.next != null) {
            if (prev.next.item == e) {
                return true;
            }
            prev = prev.next;
        }
        return false;
    }

    @Override
    public String toString() {
        if (isEmpty()) {
            return "[]";
        }
        StringBuilder str = new StringBuilder();
        str.append("[");
        Node prev = dummyHead;
        for (int i = 0; i < size; i++) {
            prev = prev.next;
            str.append(prev.toString());
            if (prev.next == null) {
                str.append("]");
            } else {
                str.append("->");
            }
        }
        return str.toString();
    }

    /**
     * 测试用例
     *
     * @param arrgs 字符串
     */
    public static void main(String[] arrgs) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            list.addFirst(i);
            System.out.println(list.toString());
        }
        list.add(3, 55);
        System.out.println(list.toString());

        list.remove(7);
        System.out.println(list.toString());

        list.add(5, 77);
        System.out.println(list.toString());

        list.remove(2);
        System.out.println(list.toString());

    }
}
